package com.heima.order.service.impl;

import com.heima.order.mapper.ServiceOrderDetailMapper;
import com.heima.order.service.ServiceOrderDetailService;
import com.heima.common.exception.CustomException;
import com.heima.model.order.dtos.ServiceOrderDetailDto;
import com.heima.model.order.pojos.ServiceOrderDetail;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class ServiceOrderDetailServiceImpl extends ServiceImpl<ServiceOrderDetailMapper, ServiceOrderDetail> implements ServiceOrderDetailService {

    /**
     * 查询列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(ServiceOrderDetailDto dto) {
        //1 参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 分页检查
        dto.checkParam();

        //2 执行查询
        // 分页
        Page page = new Page(dto.getPage(), dto.getSize());
        // 条件查询
        LambdaQueryWrapper<ServiceOrderDetail> wraaper = new LambdaQueryWrapper<>();

        // 服务项目编码
         if (StringUtils.isNotBlank(dto.getServiceCode())) {
            wraaper.like(ServiceOrderDetail::getServiceCode, dto.getServiceCode());
         }
        // 服务项目名称
         if (StringUtils.isNotBlank(dto.getServiceName())) {
            wraaper.like(ServiceOrderDetail::getServiceName, dto.getServiceName());
         }

        // 价格（分）
         if (dto.getPrice() != null) {
            wraaper.eq(ServiceOrderDetail::getPrice, dto.getPrice());
         }



        IPage pageResult = page(page, wraaper);

        //3 封装分页结果
        return PageResponseResult.page(dto.getPage(), dto.getSize(),
                (int) pageResult.getTotal(),pageResult.getRecords());
    }

    /**
     * 新增
     * @param serviceOrderDetail
     * @return
     */
    @Override
    public ResponseResult insert(ServiceOrderDetail serviceOrderDetail) {
        //1 参数检查
        if (serviceOrderDetail == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 执行保存
        save(serviceOrderDetail);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(ServiceOrderDetail serviceOrderDetail) {

        //1.检查参数
        if(null == serviceOrderDetail){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        updateById(serviceOrderDetail);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Long id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
